﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Products;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class CategoriesController : ControllerBase
    {
        #region Index
        //
        // GET: /Categories/

        public ActionResult Index()
        {
            IEnumerable<Category> categories = null;
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                categories = productService.GetCategoryTree();
                productService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorCategoryTree, MessageType.Error);
            }
            return View(categories);
        } 
        #endregion

        #region Details
        //
        // GET: /Categories/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                Category category = productService.GetCategory(id);
                productService.Close();
                return View(category);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.CategoryNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        } 
        #endregion

        #region Create
        //
        // GET: /Categories/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Categories/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.StoreCategory(category);
                productService.Close();
                this.SetMessage(InfoStrings.CategoryCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorCategoryCreating, MessageType.Error);
            }
            return View(category);
        } 
        #endregion

        #region Edit
        //
        // GET: /Categories/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Categories/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Category category)
        {
            try
            {
                category.Id = id;
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.StoreCategory(category);
                productService.Close();
                this.SetMessage(InfoStrings.CategoryUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorCategoryUpdating, MessageType.Error);
                
            }
            return View(category);
        } 
        #endregion

        #region Delete
        //
        // GET: /Categories/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Categories/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.RemoveCategory(id);
                productService.Close();
                this.SetMessage(InfoStrings.CategoryDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorCategoryDeleting, MessageType.Error);
               
            } 
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
